home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / DrawSprocket / OGL_DSp.c next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  5.3 KB  |  245 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. #include <Types.h>
  4. #include <Memory.h>
  5. #include <Quickdraw.h>
  6. #include <Fonts.h>
  7. #include <Events.h>
  8. #include <Menus.h>
  9. #include <Windows.h>
  10. #include <TextEdit.h>
  11. #include <Dialogs.h>
  12. #include <OSUtils.h>
  13. #include <ToolUtils.h>
  14. #include <SegLoad.h>
  15. #include <Sound.h>
  16.  
  17. #include "DrawSprocket.h"
  18. #include "agl.h"
  19.  
  20. #include <math.h>
  21.  
  22. #define SCREEN_WIDTH 400
  23. #define SCREEN_HEIGHT 400
  24.  
  25. /* globals */
  26. DSpContextAttributes gTheContextAttributes;
  27. DSpContextReference gTheContext;
  28.  
  29. /* static function prototypes */
  30. static CGrafPtr SetupScreen(void);
  31. static void ShutdownScreen(CGrafPtr theFrontBuffer);
  32. static AGLContext setupAGL(AGLDrawable win);
  33. static void cleanupAGL(AGLContext ctx);
  34. static void drawGL(AGLContext ctx);
  35.  
  36. /* source code */
  37. void main(void)
  38. {
  39.     CGrafPtr theScreen;
  40.     AGLContext ctx;
  41.  
  42.     /*******/
  43.  
  44.     InitGraf(&qd.thePort);
  45.     InitFonts();
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(nil);
  50.     InitCursor();
  51.     
  52.     HideCursor();
  53.     
  54.     theScreen = SetupScreen();
  55.     
  56.     /* Setup the OpenGL context */
  57.     ctx = setupAGL((AGLDrawable) theScreen);
  58.     if(!ctx) return;
  59.  
  60.     do {
  61.         /* Do the OpenGL drawing */
  62.         drawGL(ctx);
  63.     } while (!Button());
  64.     
  65.     /* Cleanup the OpenGL context */
  66.     cleanupAGL(ctx);
  67.  
  68.     ShutdownScreen(theScreen);
  69.     
  70.     ShowCursor();
  71.     
  72.     FlushEvents(everyEvent, 0);
  73.     
  74.     ExitToShell();
  75. }
  76.  
  77.  
  78. static CGrafPtr SetupScreen(void)
  79. {
  80.     OSStatus theError;
  81.  
  82.     CGrafPtr theFrontBuffer;
  83.  
  84.     theError = DSpStartup();
  85.     if( theError )
  86.         DebugStr("\pUnable to startup\n");
  87.         
  88.     gTheContextAttributes.displayWidth            = SCREEN_WIDTH;
  89.     gTheContextAttributes.displayHeight            = SCREEN_HEIGHT;
  90.     gTheContextAttributes.colorNeeds            = kDSpColorNeeds_Require;
  91.     gTheContextAttributes.displayDepthMask        = kDSpDepthMask_16;
  92.     gTheContextAttributes.displayBestDepth        = 16;
  93.     gTheContextAttributes.pageCount                = 1;
  94.     
  95.     theError = DSpFindBestContext( &gTheContextAttributes, &gTheContext );
  96.     if( theError )
  97.         DebugStr("\pUnable to find a suitable device\n");
  98.     
  99.     theError = DSpContext_Reserve( gTheContext, &gTheContextAttributes );
  100.     if( theError )
  101.         DebugStr("\pUnable to create the display!");
  102.  
  103.     theError = DSpContext_FadeGammaOut( NULL, NULL );    
  104.     if( theError )
  105.         DebugStr("\pUnable to fade the display!");
  106.  
  107.     theError = DSpContext_SetState( gTheContext, kDSpContextState_Active );
  108.     if( theError )
  109.         DebugStr("\pUnable to set the display!");
  110.         
  111.     theError = DSpContext_FadeGammaIn( NULL, NULL );    
  112.     if( theError )
  113.         DebugStr("\pUnable to fade the display!");
  114.         
  115.     {
  116.         // create a window to draw into
  117.         Rect r;
  118.         AuxWinHandle awh;
  119.         r.top = r.left = 0;
  120.         DSpContext_LocalToGlobal(gTheContext, (Point *)&r);
  121.         r.right = r.left + SCREEN_WIDTH;
  122.         r.bottom = r.top + SCREEN_HEIGHT;
  123.         
  124.         theFrontBuffer = (CGrafPtr)NewCWindow (NULL, &r, "\p", 0, plainDBox, (WindowPtr)-1, 0, 0);
  125.         
  126.         // set the content color of the window to black to avoid a white flash when the window appears.
  127.         if(GetAuxWin ((WindowPtr)theFrontBuffer, &awh)){
  128.             CTabHandle theColorTable;
  129.             OSErr err;
  130.             
  131.             /*****/
  132.             
  133.             theColorTable = (**awh).awCTable;
  134.             err = HandToHand((Handle*)&theColorTable);
  135.             if(err)
  136.                 DebugStr("\pOut of memory!");
  137.  
  138.             (**theColorTable).ctTable[wContentColor].rgb.red = 0;
  139.             (**theColorTable).ctTable[wContentColor].rgb.green = 0;
  140.             (**theColorTable).ctTable[wContentColor].rgb.blue = 0;
  141.             
  142.             CTabChanged(theColorTable);
  143.             
  144.             // the color table will be disposed by the window manager when the window is disposed
  145.             SetWinColor((WindowPtr)theFrontBuffer, (WCTabHandle)theColorTable);
  146.             
  147.         }
  148.         ShowWindow((GrafPtr)theFrontBuffer);
  149.         SetPort((GrafPtr)theFrontBuffer);
  150.         
  151.         {
  152.             RGBColor b = { 0xFFFF, 0xFFFF, 0xFFFF };
  153.             RGBColor f = { 0x0000, 0x0000, 0x0000 };
  154.             
  155.             RGBForeColor(&f);
  156.             RGBBackColor(&b);
  157.         }
  158.     }
  159.     return theFrontBuffer;
  160. }
  161.  
  162. static void ShutdownScreen(CGrafPtr theFrontBuffer)
  163. {
  164.     DSpContext_FadeGammaOut( NULL, NULL );
  165.     DisposeWindow((WindowPtr)theFrontBuffer);
  166.     DSpContext_SetState( gTheContext, kDSpContextState_Inactive );
  167.     DSpContext_FadeGammaIn( NULL, NULL );
  168.     DSpContext_Release( gTheContext );
  169.     DSpShutdown();
  170. }
  171.  
  172. /*
  173. ** OpenGL Setup
  174. */
  175. static AGLContext setupAGL(AGLDrawable win)
  176. {
  177.     GLint          attrib[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_NONE };
  178.     AGLPixelFormat fmt;
  179.     AGLContext     ctx;
  180.     GLboolean      ok;
  181.  
  182.     /* Choose an rgb pixel format */
  183.     fmt = aglChoosePixelFormat(NULL, 0, attrib);
  184.     if(fmt == NULL) return NULL;
  185.  
  186.     /* Create an AGL context */
  187.     ctx = aglCreateContext(fmt, NULL);
  188.     if(ctx == NULL) return NULL;
  189.  
  190.     /* Attach the window to the context */
  191.     ok = aglSetDrawable(ctx, win);
  192.     if(!ok) return NULL;
  193.     
  194.     /* Make the context the current context */
  195.     ok = aglSetCurrentContext(ctx);
  196.     if(!ok) return NULL;
  197.  
  198.     /* Pixel format is no longer needed */
  199.     aglDestroyPixelFormat(fmt);
  200.  
  201.     return ctx;
  202. }
  203.  
  204. /*
  205. ** OpenGL Cleanup
  206. */
  207. static void cleanupAGL(AGLContext ctx)
  208. {
  209.     aglSetCurrentContext(NULL);
  210.     aglSetDrawable(ctx, NULL);
  211.     aglDestroyContext(ctx);
  212. }
  213.  
  214. /*
  215. ** OpenGL Drawing
  216. */
  217. static void drawGL(AGLContext ctx)
  218. {
  219.     static float f, s, c;
  220.  
  221.     f += 0.01;
  222.     s = sin(f);
  223.     c = cos(f);
  224.  
  225.     /* Clear color buffer to yellow */
  226.     glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
  227.     glClear(GL_COLOR_BUFFER_BIT);
  228.     
  229.     /* Draw a smooth shaded polygon */
  230.     glBegin(GL_POLYGON);
  231.     glColor3d(1.0, 0.0, 0.0);
  232.     glVertex3d(s, c, 0.0);
  233.     glColor3d(0.0, 1.0, 0.0);
  234.     glVertex3d(-c, s, 0.0);
  235.     glColor3d(0.0, 0.0, 1.0);
  236.     glVertex3d(-s, -c, 0.0);
  237.     glColor3d(1.0, 0.0, 1.0);
  238.     glVertex3d(c, -s, 0.0);
  239.     glEnd();
  240.  
  241.     /* Ensure completion */
  242.     //glFinish();
  243.     aglSwapBuffers(ctx);
  244. }
  245.